<template>
  <div class="order-detail-container">
    <el-card class="order-card" shadow="never">
      <!-- 订单头部 -->
      <div class="order-header">
        <div class="order-id">订单号：<span>{{ order.id }}</span></div>
        <el-tag :type="tagType(order.status)" class="order-status">{{ order.status }}</el-tag>
      </div>

      <el-divider></el-divider>

      <!-- 课程信息 -->
      <div class="course-section">
        <img
          :src="order.courseCover"
          alt="课程封面"
          class="course-cover"
          @click="goToCourse(order.courseId)"
        />
        <div class="course-info">
          <h2 class="course-title" @click="goToCourse(order.courseId)">{{ order.courseTitle }}</h2>
          <p>讲师：{{ order.teacherName }}</p>
          <p>下单时间：{{ formatDate(order.gmtCreate) }}</p>
          <div class="course-price">￥{{ order.totalPrice.toFixed(2) }}</div>
        </div>
      </div>

      <el-divider></el-divider>

      <!-- 用户信息 -->
      <div class="user-section">
        <div class="user-info">
          <p><strong>用户名：</strong>{{ order.userName }}</p>
          <p><strong>邮箱：</strong>{{ order.userEmail }}</p>
        </div>
      </div>

      <el-divider></el-divider>

      <!-- 操作按钮 -->
      <div class="action-buttons">
        <el-button
          type="primary"

          @click="payNow"
          :loading="loading"
          size="medium"
        >
          {{buyOrwatch}}
        </el-button>
        <el-button @click="$router.push('/')" size="medium">返回首页</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import orderApi from '@/api/order';

export default {
  data() {
    return {
      order: {},
      loading: false,
      buyOrwatch: '立即购买'
    };
  },
  async asyncData({params, error}) {
    try {
      const res = await orderApi.getOrderInfo(params.orderId);
      if (res.data.code !== 20000) throw new Error(res.data.message);
      return {order: res.data.data};
    } catch (err) {
      error({statusCode: 500, message: '加载订单失败'});
    }
  },
  mounted() {
    if (this.order.status !== '未支付') {
      this.$message.success('购买成功！')
      this.buyOrwatch = '立即观看'
    }
  },
  methods: {
    formatDate(dateStr) {
      const d = new Date(dateStr);
      return d.toLocaleString();
    },
    tagType(status) {
      switch (status) {
        case '未支付':
          return 'warning';
        case '已支付':
          return 'success';
        case '已取消':
          return 'info';
        default:
          return 'info';
      }
    },
    payNow() {
      this.loading = true;
      // 支付跳转
      setTimeout(() => {
        this.loading = false;
        this.$message.success('跳转支付页...');
        this.$router.push(`/pay/${this.order.id}`);
      }, 600);
    },
    goToCourse(courseId) {
      this.$router.push(`/course/${courseId}`);
    },
  },
};
</script>

<style scoped>
.order-detail-container {
  max-width: 900px;
  margin: 40px auto;
  padding: 20px;
  background-color: #f5f7fa;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue",
  Arial, sans-serif;
}

.order-card {
  padding: 30px 40px;
  border-radius: 8px;
  box-shadow: none;
  background-color: #fff;
  border: 1px solid #e8e8e8;
}

.order-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 16px;
  color: #333;
  font-weight: 600;
}

.order-id span {
  font-family: monospace;
  color: #606266;
  user-select: all;
}

.order-status {
  font-weight: 600;
  padding: 4px 16px;
  border-radius: 20px;
  font-size: 14px;
}

.course-section {
  display: flex;
  gap: 30px;
  align-items: center;
}

.course-cover {
  width: 210px;
  height: 120px;
  border-radius: 6px;
  object-fit: cover;
  cursor: pointer;
  box-shadow: 0 2px 8px rgb(0 0 0 / 0.1);
  transition: transform 0.3s ease;
}

.course-cover:hover {
  transform: scale(1.03);
}

.course-info {
  flex: 1;
  color: #444;
  position: relative;
}

.course-title {
  font-size: 22px;
  font-weight: 700;
  margin-bottom: 10px;
  cursor: pointer;
  color: #303133;
  user-select: text;
}

.course-title:hover {
  color: #409eff;
  text-decoration: underline;
}

.course-info p {
  margin: 4px 0;
  font-size: 15px;
  color: #666;
}

.course-price {
  position: absolute;
  bottom: 0;
  right: 0;
  font-size: 24px;
  font-weight: 700;
  color: #f56c6c;
}

.user-section {
  font-size: 15px;
  color: #444;
}

.user-info p {
  margin: 6px 0;
}

.action-buttons {
  margin-top: 30px;
  display: flex;
  gap: 15px;
  justify-content: flex-end;
}

.el-button {
  border-radius: 4px;
  font-weight: 600;
  padding: 8px 28px;
}

.el-button[type="primary"] {
  background-color: #409eff;
  border-color: #409eff;
}

.el-button[type="primary"]:hover {
  background-color: #66b1ff;
  border-color: #66b1ff;
}

.el-button[disabled] {
  background-color: #c0c4cc !important;
  border-color: #c0c4cc !important;
  color: #909399 !important;
  cursor: not-allowed !important;
}

.order-detail-container {
  max-width: 900px;
  margin: 40px auto;
  padding: 20px;
  background-color: #f5f7fa; /* 浅灰色背景，更柔和自然 */
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue",
  Arial, sans-serif;
}

.order-card {
  padding: 30px 40px;
  border-radius: 8px;
  background-color: #fff;
  border: 1px solid #e1e4eb; /* 浅灰边框 */
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1); /* 柔和阴影，突出层次 */
}


</style>
